package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.bean.BannerInfo;
import com.gensdai.leliang.view.banner.PageInfo;
import com.gensdai.leliang.view.banner.SimpleImageBanner;
import com.gensdai.leliang.view.banner.widget.Banner.base.BaseBanner;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页banner适配
 * Created by LT on 2017/6/15.
 */

public class BannerAdapterRec extends DelegateAdapter.Adapter<BannerAdapterRec.MainViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;

    BannerInfo blist;

    OnBannerItemClick click;

    public BannerAdapterRec(Context context, LayoutHelper layoutHelper, BannerInfo list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public BannerAdapterRec(Context context, LayoutHelper layoutHelper, BannerInfo list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    public void setOnBannerItemClick(OnBannerItemClick click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_main_banner_rec, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));
        holder.mainBanner.setSource(blist.getInfos()).startScroll();
        holder.mainBanner.setOnItemClickL(new BaseBanner.OnItemClickL() {
            @Override
            public void onItemClick(int position) {
                PageInfo info = blist.getInfos().get(position);
                if (click != null){
                    click.OnItemClick(position,info);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return 1;
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.mainBanner)
        SimpleImageBanner mainBanner;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


    public interface OnBannerItemClick {
        void OnItemClick(int position, PageInfo info);
    }
}
